Page({
  data: {
    latitude: 0,
    longitude: 0,
    show: false,
    markers: [],
    hasLocationPermission: false, 
    selectedType: 'public_place',
    class: [
      { type: 'gate', title: '大门' },
      { type: 'public_place', title: '公共场地'},
      { type: 'restaurant', title: '餐厅' },
      { type: 'academy', title: '学院' },
      { type: 'apartment', title: '宿舍' },
      { type: 'study', title: '教学楼' }
    ],
  },

  onLoad() {
    this.checkLocationPermission();
  },

  changeType(e) {
    const { type } = e.currentTarget.dataset;
    this.setData({
      selectedType: type
    });
    this.filterMarkersByType();
  },
  
  checkLocationPermission() {
    const that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation']) {
          that.setData({
            hasLocationPermission: true
          });
          that.getLocation();
        } else {
          wx.authorize({
            scope: 'scope.userLocation'
          });
        }
      },
      fail() {
        wx.openSetting({
          success() {
            that.checkLocationPermission();
          }
        });
      }
    });
  },

  getLocation() {
    const that = this; //保存上下文
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success(res) {
        // 更新实时位置
        const newMarkers = [
          {
            id: 0,
            latitude: res.latitude,
            longitude: res.longitude,
            type: "my_location",
            title: '我的位置',
            width: 20, // 图标的宽度
            height: 25 // 图标的高度
          },
          // 大门
          {
            id: 1,
            latitude: 37.470452,
            longitude: 118.543100,
            type: "gate",
            title: '北门',
            width: 18,
            height: 23
          },
          {
            id: 2,
            latitude: 37.465184,
            longitude: 118.551647,
            type: "gate",
            title: '东门',
            width: 18,
            height: 23
          },
          {
            id: 3,
            latitude: 37.465486,
            longitude: 118.536288,
            type: "gate",
            title: '西门',
            width: 18,
            height: 23
          },
          // 公共场地
          {
            id: 4,
            latitude: 37.465315,
            longitude: 118.550266,
            type: "public_place",
            title: '体育馆',
            width: 18,
            height: 23
          },
          {
            id: 5,
            latitude: 37.468554,
            longitude: 118.543093,
            type: "public_place",
            title: '图书馆',
            width: 18,
            height: 23
          },
          {
            id: 6,
            latitude: 37.466676,
            longitude: 118.549500,
            type: "public_place",
            title: '东西操场',
            width: 18,
            height: 23
          },
          {
            id: 7,
            latitude: 37.468731,
            longitude: 118.547542,
            type: "public_place",
            title: '羽毛球馆',
            width: 18,
            height: 23
          },
          {
            id: 8,
            latitude: 37.466380,
            longitude: 118.540890,
            type: "public_place",
            title: '主楼',
            width: 18,
            height: 23
          },
          {
            id: 9,
            latitude: 37.466789,
            longitude: 118.546539,
            type: "public_place",
            title: '菜鸟驿站',
            width: 18,
            height: 23
          },
          {
            id: 10,
            latitude: 37.466036,
            longitude: 118.544291,
            type: "public_place",
            title: '音乐厅',
            width: 18,
            height: 23
          },
          {
            id: 11,
            latitude: 37.465708,
            longitude: 118.544656,
            type: "public_place",
            title: '排练厅',
            width: 18,
            height: 23
          },
          {
            id: 12,
            latitude: 37.467803,
            longitude: 118.544865,
            title: '大学生创新实践中心',
            type: "public_place",
            width: 18,
            height: 23
          },
          {
            id: 13,
            latitude: 37.467930,
            longitude: 118.541979,
            title: '100号报告厅',
            type: "public_place",
            width: 18,
            height: 23
          },
          {
            id: 14,
            latitude: 37.468650,
            longitude: 118.541202,
            type: "public_place",
            title: '艺术楼',
            width: 18,
            height: 23
          },
          {
            id: 15,
            latitude: 37.466534,
            longitude: 118.541808,
            type: "public_place",
            title: '校医护室',
            width: 18,
            height: 23
          },
          {
            id: 16,
            latitude: 37.467930,
            longitude: 118.546539,
            type: "public_place",
            title: '美发沙龙',
            width: 18,
            height: 23
          },
          // 餐厅
          {
            id: 17,
            latitude: 37.466491,
            longitude: 118.545507,
            type: "restaurant",
            title: '第二餐厅',
            width: 18,
            height: 23
          },
          {
            id: 18,
            latitude: 37.468377,
            longitude: 118.546351,
            type: "restaurant",
            title: '第三餐厅',
            width: 18,
            height: 23
          },
          {
            id: 19,
            latitude: 37.467006,
            longitude: 118.547301,
            type: "restaurant",
            title: '第五餐厅',
            width: 18,
            height: 23
          },
          // 学院
          {
            id: 20,
            latitude: 37.466977,
            longitude: 118.544302,
            type: 'academy',
            title: '化学工程学院',
            width: 18,
            height: 23
          },
          {
            id: 21,
            latitude: 37.468407,
            longitude: 118.544415,
            type: 'academy',
            title: '智能制造与控制工程学院',
            width: 18,
            height: 23
          },
          {
            id: 22,
            latitude: 37.467866,
            longitude: 118.541529,
            type: 'academy',
            title: '教育与现代媒体学院(第一教学楼)',
            width: 18,
            height: 23
          },
          {
            id: 23,
            latitude: 37.466930,
            longitude: 118.539710,
            type: 'academy',
            title: '经济管理与文法学院',
            width: 18,
            height: 23
          },
          {
            id: 24,
            latitude: 37.466568,
            longitude: 118.539716,
            type: 'academy',
            title: '生物医药与护理学院',
            width: 18,
            height: 23
          },
          {
            id: 25,
            latitude: 37.466291,
            longitude: 118.540054,
            type: 'academy',
            title: '石油工程学院',
            width: 18,
            height: 23
          },
          // 教学楼
          {
            id: 26,
            latitude: 37.466870,
            longitude: 118.542044,
            type: 'study',
            title: '第二教学楼',
            width: 18,
            height: 23
          },
          {
            id: 27,
            latitude: 37.467138,
            longitude: 118.540359,
            type: 'study',
            title: '第三教学楼',
            width: 18,
            height: 23
          },
          {
            id: 28,
            latitude: 37.467619,
            longitude: 118.539185,
            type: 'study',
            title: '第四教学楼',
            width: 18,
            height: 23
          },
          // 宿舍
          {
            id: 29,
            latitude: 37.465678,
            longitude: 118.545531,
            type: 'apartment',
            title: '学0',
            width: 18,
            height: 23
          },
          {
            id: 30,
            latitude: 37.466849,
            longitude: 118.545600,
            type: 'apartment',
            title: '学1',
            width: 18,
            height: 23
          },
          {
            id: 31,
            latitude: 37.467266,
            longitude: 118.545971,
            type: 'apartment',
            title: '学2',
            width: 18,
            height: 23
          },
          {
            id: 32,
            latitude: 37.467641,
            longitude: 118.545686,
            type: 'apartment',
            title: '学3',
            width: 18,
            height: 23
          },
          {
            id: 33,
            latitude: 37.468045,
            longitude: 118.545681,
            type: 'apartment',
            title: '学4',
            width: 18,
            height: 23
          },
          {
            id: 34,
            latitude: 37.468497,
            longitude: 118.545493,
            type: 'apartment',
            title: '学5',
            width: 18,
            height: 23
          },
          {
            id: 35,
            latitude: 37.468829,
            longitude: 118.545515,
            type: 'apartment',
            title: '学6',
            width: 18,
            height: 23
          },
          {
            id: 36,
            latitude: 37.466419,
            longitude: 118.546512,
            type: 'apartment',
            title: '学7',
            width: 18,
            height: 23
          },
          {
            id: 37,
            latitude: 37.469314,
            longitude: 118.547038,
            type: 'apartment',
            title:'学8',
            width: 18,
            height: 23
          },
          {
            id: 38,
            latitude: 37.465576,
            longitude: 118.548545,
            type: 'apartment',
            title: '学10',
            width: 18,
            height: 23
          },
          {
            id: 39,
            latitude: 37.465967,
            longitude: 118.548519,
            type: 'apartment',
            title: '学11',
            width: 18,
            height: 23
          },
          {
            id: 40,
            latitude: 37.466363,
            longitude: 118.547548,
            type: 'apartment',
            title: '学12',
            width: 18,
            height: 23
          },
          {
            id: 41,
            latitude: 37.466615,
            longitude: 118.548213,
            type: 'apartment',
            title: '学13',
            width: 18,
            height: 23
          },
          {
            id: 42,
            latitude: 37.467113,
            longitude: 118.548165,
            type: 'apartment',
            title: '学14',
            width: 18,
            height: 23
          },
          {
            id: 43,
            latitude: 37.467441,
            longitude: 118.548170,
            type: 'apartment',
            title: '学15',
            width: 18,
            height: 23
          },
        ];
        
        // 为每个标记设置图标路径
      const processedMarkers = newMarkers.map(marker => {
        let iconPath;
        switch (marker.type) {
          case 1:
            iconPath = "../../../resources/my_location.png";
            break;
          case "gate":
            iconPath = "../../../resources/gate.png";
            break;
          case "public_place":
            iconPath = "../../../resources/public_place.png";
            break;
          case "restaurant":
            iconPath = "../../../resources/restaurant.png";
            break;
          case "academy":
            iconPath = "../../../resources/academy.png";
            break;
          case "apartment":
            iconPath = "../../../resources/apartment.png";
            break;
          case "study":
            iconPath = "../../../resources/study.png";
            break;
          default:
            iconPath = "../../../resources/my_location.png"; // 默认图标
        }
        return {
          ...marker,
          iconPath
        };
      });

      // 存储所有标记
      that.setData({
        allMarkers: processedMarkers
      });

      // 根据 selectedType 筛选标记
      that.filterMarkersByType();

      that.setData({
        latitude: res.latitude,
        longitude: res.longitude,
        markers: processedMarkers
      });
      },
      fail(err) {
        console.error('获取位置失败', err);
        wx.showToast({
          title: '获取位置失败,请重新定位',
          icon: 'none'
        });
      },
    });
  },

  filterMarkersByType() {
    const { allMarkers, selectedType } = this.data;
    const myLocationMarker = allMarkers.find(marker => marker.type === 'my_location');
    const filteredMarkers = allMarkers.filter(marker => marker.type === selectedType);
    const finalMarkers = [myLocationMarker, ...filteredMarkers];
    this.setData({
      markers: finalMarkers
    });
  },

  showPopup(e) {
    const markerId = e.detail.markerId  
    this.setData({ 
      show: true,
      currentMarker: this.data.markers.find(item  => item.id  === markerId)
    })
  },


  onClose() {
    this.setData({ show: false });
  },

  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() { }
})